/*
   Copyright 2011 marcopar@gmail.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package eu.flatworld.commons.log;

import java.awt.Dialog.ModalityType;

public class LogXDialog extends javax.swing.JDialog {

    public LogXDialog(String title, String message, Throwable exception) {
        super();
        initComponents();
        setModalityType(ModalityType.MODELESS);
        setAlwaysOnTop(true);

        if (title != null) {
            setTitle(title);
        } else {
            setTitle("");
        }
        if (message != null) {
            jpMessage.setVisible(true);
            jlMessage.setText(message);
        } else {
            jpMessage.setVisible(false);
        }

        if (exception != null) {
            jspStacktrace.setVisible(true);
            jtaStacktrace.setText(LogX.exceptionToString(exception));
        } else {
            jspStacktrace.setVisible(false);
        }
        jtaStacktrace.setCaretPosition(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        stackTracePopup = new javax.swing.JPopupMenu();
        jmiCopy = new javax.swing.JMenuItem();
        jspStacktrace = new javax.swing.JScrollPane();
        jtaStacktrace = new javax.swing.JTextArea();
        jpMessage = new javax.swing.JPanel();
        jlMessage = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jbClose = new javax.swing.JButton();

        jmiCopy.setText("Copy all");
        jmiCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiCopyActionPerformed(evt);
            }
        });
        stackTracePopup.add(jmiCopy);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jtaStacktrace.setColumns(20);
        jtaStacktrace.setComponentPopupMenu(stackTracePopup);
        jtaStacktrace.setEditable(false);
        jtaStacktrace.setRows(5);
        jspStacktrace.setViewportView(jtaStacktrace);

        jlMessage.setText(" ");

        javax.swing.GroupLayout jpMessageLayout = new javax.swing.GroupLayout(jpMessage);
        jpMessage.setLayout(jpMessageLayout);
        jpMessageLayout.setHorizontalGroup(
            jpMessageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpMessageLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
                .addContainerGap())
        );
        jpMessageLayout.setVerticalGroup(
            jpMessageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpMessageLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jbClose.setText("Close");
        jbClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(306, Short.MAX_VALUE)
                .addComponent(jbClose)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jbClose)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jpMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jspStacktrace, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jpMessage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspStacktrace, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-392)/2, (screenSize.height-289)/2, 392, 289);
    }// </editor-fold>//GEN-END:initComponents
    private void jbCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCloseActionPerformed
        dispose();
    }//GEN-LAST:event_jbCloseActionPerformed

    private void jmiCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiCopyActionPerformed
        jtaStacktrace.selectAll();
        jtaStacktrace.copy();
    }//GEN-LAST:event_jmiCopyActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbClose;
    private javax.swing.JLabel jlMessage;
    private javax.swing.JMenuItem jmiCopy;
    private javax.swing.JPanel jpMessage;
    private javax.swing.JScrollPane jspStacktrace;
    private javax.swing.JTextArea jtaStacktrace;
    private javax.swing.JPopupMenu stackTracePopup;
    // End of variables declaration//GEN-END:variables
}
